package com.twxm.breed.controller;

import com.alibaba.fastjson.JSONObject;
import com.twxm.breed.entity.LoginInfo;
import com.twxm.breed.entity.User;
import com.twxm.breed.redis.ScanQrcodeRedis;
import com.twxm.breed.redis.WXRedis;
import com.twxm.breed.service.LoginAndUserService;
import com.twxm.breed.service.LoginInfoService;
import com.twxm.breed.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.transaction.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionSystemException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
@RequestMapping("/wx")
@Slf4j
public class ScanCodeController {
    @Autowired
    private ScanQrcodeRedis scanQrcodeRedis;
    @Autowired
    private WXRedis wxRedis;
    @Autowired
    private LoginInfoService loginInfoService;
    @Autowired
    private LoginAndUserService loginAndUserService;
    @Autowired
    private UserService userService;

    @RequestMapping("/doLogin")
    public JSONObject doLogin(){
        log.info("前往登入页面");
        JSONObject jsonObject = new JSONObject();
        //生成随机id
        String key = UUID.randomUUID().toString();

        jsonObject.put("id",key);
        //标记当前二维码未被扫描
        scanQrcodeRedis.set(key,0);

        return jsonObject;
    }

    @RequestMapping("/scan_result")
    public JSONObject scanResult(HttpServletRequest request){
        log.info("正在检查扫码情况");
        JSONObject jsonObject = new JSONObject();
        String scanKey = request.getParameter("id");
        log.info("scanKey:" + scanKey);
        Object openId = scanQrcodeRedis.get(scanKey);
        log.info("openid:" + openId);
        if (openId == null){
            jsonObject.put("res","false");
            return jsonObject;
        }
        Object wxValue =  wxRedis.get(openId.toString());
        log.info("wxValue:" + wxValue);
        if (wxValue == null){
            jsonObject.put("res","continue");
            return jsonObject;
        }
        if (wxValue.equals("t")){
            jsonObject.put("res","login");//用户扫码登录
            User user = loginInfoService.getUserByOpenId(openId.toString());
            jsonObject.put("userid", user.getId());
            jsonObject.put("username", user.getName());
        }else if (wxValue.equals("f")){
            jsonObject.put("res","register");//用户需要跳转
        }else{
            jsonObject.put("res","continue");
        }
        return jsonObject;
    }

    @RequestMapping("/doRegister")
    public JSONObject doRegister(HttpServletRequest request){
        log.info("正在注册。");
        String scanKey = request.getParameter("id");
        String userid = request.getParameter("userid");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String openId = scanQrcodeRedis.get(scanKey).toString();
        User user = new User(userid,username);
        LoginInfo loginInfo = new LoginInfo(openId,user,password,0);
        JSONObject jsonObject = new JSONObject();
        try {
            loginAndUserService.setAccount(loginInfo, user);
            jsonObject.put("res","true");//注册成功
            jsonObject.put("userid",userid);
            jsonObject.put("username",username);
        }catch (Exception e) {
            log.info("发生回滚");
            e.printStackTrace();
            jsonObject.put("res","false");
        }
        return jsonObject;
    }

    @RequestMapping("/search_userid")
    public JSONObject searchUserId(HttpServletRequest request){
        log.info("查询用户id。。");
        JSONObject jsonObject = new JSONObject();
        String userId = request.getParameter("userid");
        if (loginInfoService.isExistUserId(userId) == null){
            jsonObject.put("res","noExist");
        }else{
            jsonObject.put("res","isExist");
        }
        return jsonObject;
    }

    @RequestMapping("login_by_id")
    public JSONObject loginById(HttpServletRequest request){
        log.info("检查用户账号密码是否正确");
        String userId = request.getParameter("userid");
        String password = request.getParameter("password");
        JSONObject jsonObject = new JSONObject();
        if (loginInfoService.getPassword(userId) == null || !loginInfoService.getPassword(userId).equals(password)){
            jsonObject.put("res","false");
        }else{
            String userName = userService.selectNameById(userId);
            jsonObject.put("userid",userId);
            jsonObject.put("username",userName);
            jsonObject.put("res","true");
        }
        return jsonObject;
    }
}

